package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.bo.*;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.GoodsListPO;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.service.GoodsService;
import org.apache.commons.lang3.StringUtils;
import com.cskaoyan.util.StringToArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * zx
 */

@RestController
@RequestMapping("admin/goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;

    @RequestMapping("list")
    public BaseRespVo list(BaseParam baseParam, Integer goodsId, String goodsSn, String name) {
        GoodsVO goodsVO = goodsService.queryGoods(baseParam, goodsId, goodsSn, name);
        return BaseRespVo.ok(goodsVO);
    }

    @RequestMapping("create")
    public BaseRespVo create(@RequestBody GoodsBO goodsBO) {
        if (goodsBO.getGoods().getName() == null || goodsBO.getGoods().getGoodsSn() == null) {
            return BaseRespVo.err("数据不能为空", 200);
        }

        try {
            Integer.parseInt(goodsBO.getGoods().getGoodsSn());
        } catch (Exception e) {
            return BaseRespVo.err("参数格式错误,请重新输入!", 200);
        }


        List<GoodsAttributeBO> attributes = goodsBO.getAttributes();
        for (GoodsAttributeBO attribute : attributes) {
            if (attribute.getAttribute() == null || attribute.getValue() == null) {
                return BaseRespVo.err("商品参数格式错误,请重新输入!", 200);
            }

        }


        GoodsCreateVO createVO = goodsService.createGoods(goodsBO);
        return BaseRespVo.ok(null);
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody GoodsUpdateGetBO goodsUpdateGetBO) {
        GoodsUpdateVO goodsUpdateVO = goodsService.updateGoods(goodsUpdateGetBO);
        return BaseRespVo.ok(null);
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody GoodsListPO goodListBO) {
        goodsService.deleteGoods(goodListBO);
        return BaseRespVo.ok(null);
    }

    @RequestMapping("detail")
    public BaseRespVo detail(Integer id) {
        GoodsDetailVO goodsDetailVO = goodsService.detailGoods(id);
        return BaseRespVo.ok(goodsDetailVO);
    }

    @RequestMapping("catAndBrand")
    public BaseRespVo catAndBrand() {
        GoodsCatAndBrandVO catAndBrandVO = goodsService.getCatAndBrand();
        return BaseRespVo.ok(catAndBrandVO);
    }
}
